﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;



using PowerCreator.LMS.Web.Question;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Question.Entiry;
public partial class sysadmin_QuestionManager_AddPaperCelue :System.Web.UI.Page// PowerCreator.LMS.Web.Base.BasePage
{
    ManagerPaper objManagerPaper = new ManagerPaper();
    ManagerQuestion objManagerQuestion = new ManagerQuestion();
    Course objManagerCourse = new Course();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //InitPage();

            BandGV();
        }
    }
    //protected override void InitPage()
    //{
    //    lab_AddPaperCelue_Title.Text = GetLanageValueByKey("lab_AddPaperCelue_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
    //    lab_AddPaperCelue_Name.Text = GetLanageValueByKey("lab_AddPaperCelue_CelueName", PowerCreator.LMS.Language.LanagetItemType.Page);
    //    btn_AddPaperCelue_add.Text = GetLanageValueByKey("btn_AddPaperCelue_AddCelue", PowerCreator.LMS.Language.LanagetItemType.Page);
    //    lab_AddPaperCelue_AddDescription.Text = GetLanageValueByKey("lab_AddPaperCelue_Msg", PowerCreator.LMS.Language.LanagetItemType.Page);

    //    base.InitPage();
    //}
    private void BandGV()
    {
        DataTable dt_gv = new DataTable();
        dt_gv.Columns.Add("ID");

        DataTable dt_Course = new DataTable();
        dt_Course = objManagerCourse.QueryCourse("");


        for (int i = 0; i < 6; i++)
        {
            DataRow dr1 = dt_gv.NewRow();
            dr1["ID"] = Guid.NewGuid();

            dt_gv.Rows.Add(dr1);
        }
        gv_celue.DataSource = dt_gv;
        gv_celue.DataBind();
        for (int i = 1; i < 7; i++)
        {
            Label lab_QuestionType = (Label)gv_celue.Rows[i-1].Cells[1].FindControl("lab_QuestionType");
            Label lab_QuestionTypeID = (Label)gv_celue.Rows[i - 1].Cells[1].FindControl("lab_QuestionTypeID");
            lab_QuestionTypeID.Text = i.ToString();
            switch (i)
            {
                case 1: lab_QuestionType.Text = "判断题";
                    
                    break;
                case 2: lab_QuestionType.Text = "单选题";
                    break;
                case 3: lab_QuestionType.Text = "多选题";
                    break;
                case 4: lab_QuestionType.Text = "填空题";
                    break;
                case 5: lab_QuestionType.Text = "名词解释";
                    break;
                default:
                    lab_QuestionType.Text = "简答题";
                    break;
            }
            DropDownList dpdlst_classtype = (DropDownList)gv_celue.Rows[i-1].Cells[5].FindControl("dpdlst_classtype");
            dpdlst_classtype.DataSource = dt_Course;
            dpdlst_classtype.DataValueField = "Course_Id";
            dpdlst_classtype.DataTextField = "CourseName";
            dpdlst_classtype.DataBind();

            


        }
    }
    private bool CheckDate()
    {
        for (int i = 0; i < gv_celue.Rows.Count; i++)
        {
            CheckBox chbox_check = (CheckBox)gv_celue.Rows[i].Cells[6].FindControl("chbox_check");
            if (chbox_check.Checked)
            {
                Label lab_QuestionTypeID = (Label)gv_celue.Rows[i].Cells[1].FindControl("lab_QuestionTypeID");
                Label lab_QuestionType = (Label)gv_celue.Rows[i].Cells[1].FindControl("lab_QuestionType");

                DropDownList dpdlist_nanduzhi = (DropDownList)gv_celue.Rows[i].Cells[2].FindControl("dpdlist_nanduzhi");

                DropDownList dpdlst_classtype = (DropDownList)gv_celue.Rows[i].Cells[5].FindControl("dpdlst_classtype");

                TextBox txt_Ken = (TextBox)gv_celue.Rows[i].Cells[6].FindControl("txt_Ken");

                TextBox txt_QuestionCount = (TextBox)gv_celue.Rows[i].Cells[3].FindControl("txt_QuestionCount");
                TextBox txt_ScoreCount = (TextBox)gv_celue.Rows[i].Cells[4].FindControl("txt_ScoreCount");
                if (txt_QuestionCount.Text.Trim() != "")
                {
                    if (!PageValidate.IsNumber(txt_QuestionCount.Text))
                    {
                        Script.Alert(this, "msg", "试题数量请输入数字");
                        return false;
                    }
                }
                if (txt_ScoreCount.Text.Trim() != "")
                {
                    if (!PageValidate.IsNumber(txt_ScoreCount.Text))
                    {
                        Script.Alert(this, "msg", "试题总分请输入数字");
                        return false;
                    }
                }
                if (txt_QuestionCount.Text == "0" | txt_ScoreCount.Text == "0")
                {
                    Script.Alert(this, "msg", "试题数量或试题分数必须大于0");
                    return false;
                }
                int QuestionCount = int.Parse(txt_QuestionCount.Text);
                int Diffculty = int.Parse(dpdlist_nanduzhi.SelectedValue);
                int QuestionTypeID = int.Parse(lab_QuestionTypeID.Text);
                int ScoreCount = int.Parse(txt_ScoreCount.Text);
                int CourseID = int.Parse(dpdlst_classtype.SelectedValue);
                string Ken = txt_Ken.Text;
                if (objManagerPaper.GetAoutSelectQuestion(QuestionCount, Diffculty, QuestionTypeID, ScoreCount, CourseID, Ken).Rows.Count < QuestionCount)
                {
                    Script.Alert(this, "msg", "没有足够的试题来满足" + lab_QuestionType.Text + "策略");
                    return false;
                }

            }
        }
        return true;
    }
    protected void btn_add_Click(object sender, EventArgs e)
    {
        if (!CheckDate())
        {
            return;
        }
        VOPloy objPloyInfo = new VOPloy();
        
        objPloyInfo.PloyName = txt_PloyName.Text;
        objPloyInfo.PloyCreateID = 10000;
        objPloyInfo.PloyCreateTime = DateTime.Now;
        int PloyID = 0;
        objManagerPaper.CreatePloy(objPloyInfo,ref PloyID);

        VOPloyOption objPloyOptionInfo = new VOPloyOption();
        objPloyOptionInfo.FKPloyOptionPloyID = PloyID;
        
        for (int i = 0; i < gv_celue.Rows.Count; i++)
        {
            CheckBox chbox_check = (CheckBox)gv_celue.Rows[i].Cells[6].FindControl("chbox_check");
            if (chbox_check.Checked)
            {

                Label lab_QuestionTypeID = (Label)gv_celue.Rows[i].Cells[1].FindControl("lab_QuestionTypeID");
                objPloyOptionInfo.PloyOptionQuestionTypeID = int.Parse(lab_QuestionTypeID.Text);
                DropDownList dpdlist_nanduzhi = (DropDownList)gv_celue.Rows[i].Cells[2].FindControl("dpdlist_nanduzhi");
                objPloyOptionInfo.PloyOptionDiffculty = int.Parse(dpdlist_nanduzhi.SelectedValue);
                TextBox txt_QuestionCount = (TextBox)gv_celue.Rows[i].Cells[3].FindControl("txt_QuestionCount");
                if (!PageValidate.IsNumber(txt_QuestionCount.Text))
                {
                    objPloyOptionInfo.PloyOptionQuestionCount = 0;
                }
                else
                {
                    objPloyOptionInfo.PloyOptionQuestionCount = int.Parse(txt_QuestionCount.Text.Trim());
                }
                TextBox txt_ScoreCount = (TextBox)gv_celue.Rows[i].Cells[4].FindControl("txt_ScoreCount");
                if (!PageValidate.IsNumber(txt_ScoreCount.Text))
                {
                    objPloyOptionInfo.PloyOptionQuestionScore = 0;
                }
                else
                {
                    objPloyOptionInfo.PloyOptionQuestionScore = int.Parse(txt_ScoreCount.Text.Trim());
                }
               
                DropDownList dpdlst_classtype = (DropDownList)gv_celue.Rows[i].Cells[5].FindControl("dpdlst_classtype");
                objPloyOptionInfo.FKPloyOptionCourseID = int.Parse(dpdlst_classtype.SelectedValue);
                TextBox txt_Ken = (TextBox)gv_celue.Rows[i].Cells[6].FindControl("txt_Ken");
                objPloyOptionInfo.PloyOptionKen = txt_Ken.Text;
                objPloyOptionInfo.PloyOptionCreateID = int.Parse(Session["UserID"].ToString());
                objPloyOptionInfo.PloyOptionCreateTime = DateTime.Now;

                objManagerPaper.CreatePloyOption(objPloyOptionInfo);
            }
        }
        Script.ShowAndRedirect(this, "操作成功", "ManagerPaperPloy.aspx");
    }
}
